<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>申购单列表---申请人</title>
    <!--最早的，未进入竞价流程的-->
    <script type="text/javascript">
      var purchaseId;
      var newPurchaseWin;
      var purchaseViewWin;

      var isManager = '<%=session[:is_dept_manager]%>'
      var status = '<%=params[:auditing_status]%>'
      Ext.onReady(function(){

        var auditing_status = new Ext.form.Hidden({
          name: 'auditing_status',
          value: '<%=params[:auditing_status]%>'
        });

        var purchaseNo = new Ext.form.TextField({
          fieldLabel: '申购单号',
          allowBlank: true,
          blankText: "申购单号不能为空",
          name: 'order_number',
          anchor: '97%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          allowBlank: true,
          blankText: "产品名称",
          name: 'product_name',
          anchor: '97%'
        });

        var applicantTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<div style="float:left; width:90px">{name}</div><div style="float:left; width:70px">{login_id}</div>{is_suit}', '</div></tpl>');
        applicantTemplate.compile();
        var applicantStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/applicant/getApplicants?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            },{
              name: 'login_id',
              convert:function(value,record){
                if (value === ""){
                  return "未设置"
                }else{
                  return value;
                }
              }
            }])
        });

        var applicant =  new Ext.form.ComboBox({
          fieldLabel: '申购人',
          store: applicantStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'applicant_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          pageSize: 10,
          //hideTrigger: false,
          tpl: applicantTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          selectOnFocus:true,
          triggerAction:"all",
          anchor: '100%'
        });

        var unitPriceFrom = new Ext.form.NumberField({
          fieldLabel: '单价(从)',
          name: 'unit_price_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var unitPriceTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'unit_price_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountFrom = new Ext.form.NumberField({
          fieldLabel: '总额(从)',
          name: 'amount_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'amount_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        //搜索按钮
        var searchButton = new Ext.Button({
          text: '搜索',
          tooltip: '搜索申购记录',
          iconCls: 'finding',
          handler: function(){
            purchaseDS.baseParams.auditing_status = auditing_status.getValue();
            purchaseDS.baseParams.order_number = purchaseNo.getValue();
            purchaseDS.baseParams.product_name = productName.getValue();
            purchaseDS.baseParams.applicant_id = applicant.getValue();
            purchaseDS.baseParams.unit_price_from = unitPriceFrom.getValue();
            purchaseDS.baseParams.unit_price_to = unitPriceTo.getValue();
            purchaseDS.baseParams.amount_from = amountFrom.getValue();
            purchaseDS.baseParams.amount_to = amountTo.getValue();
            purchaseDS.load({
              params: {
                start: 0,
                limit: 15
              }
            });
          }
        })

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            searchPurchaseForm.form.reset();
          }
        });

        var searchPurchaseForm = new Ext.FormPanel({
          labelWidth: 60, // label settings here cascade unless overridden
          url:'/purchase/create',
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          width: 900,
          //height:300,
          items: [
            {
              xtype: 'fieldset',
              defaults: {width: 850},
              title: '条件搜索',
              border: true,
              items: [
                {
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [purchaseNo]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [productName]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [applicant]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [unitPriceFrom]
                    },{
                      columnWidth: .13,
                      layout: 'form',
                      border: false,
                      labelWidth: 30,
                      items: [unitPriceTo]
                    },{
                      columnWidth: .17,
                      layout: 'form',
                      border: false,
                      items: [amountFrom]
                    },{
                      columnWidth: .13,
                      layout: 'form',
                      border: false,
                      labelWidth: 30,
                      items: [amountTo]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [auditing_status]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        
        //产品列表---------------------------------------------------------
        //生成有关客户的Grid
        var purchaseDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/purchase/getPurchasesForApplicant?auditing_status=<%=params[:auditing_status]%>'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'manager'
            }, {
              name: 'order_number'
            }, {
              name: 'product_name'
            }, {
              name: 'model'
            }, {
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'estimated_price'
            }, {
              name: 'estimated_amount'
            }, {
              name: 'auditing_status'
            }, {
              name: 'dept_auditing'
            }, {
              name: 'brand'
            },{
              name: 'purchase_type'
            },{
              name: 'created_at'
            },{
              name:'applicant_name',
              mapping:'applicant.name'
            }])
        });
        var purchaseSM = new Ext.grid.CheckboxSelectionModel();
        var purchaseCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),purchaseSM,{
            header: '申购序号',
            dataIndex: 'order_number',
            width:130
          }, {
            id:'applicant',
            header: '申购人',
            dataIndex: 'applicant_name',
            width:80
          }, {
            header: '产品名称',
            dataIndex: 'product_name',
            width:180
          }, {
            header: '品牌',
            dataIndex: 'brand',
            width:60
          }, {
            header: '规格',
            dataIndex: 'specification',
            width:80
          }, {
            header: '生产商',
            dataIndex: 'manufacturer',
            width:90
          }, {
            header: '预估单价',
            dataIndex: 'estimated_price',
            renderer: Ext.util.Format.numberRenderer('0,000.00'),
            align: 'right',
            width:70
          }, {
            header: '预估总额',
            dataIndex: 'estimated_amount',
            renderer: Ext.util.Format.numberRenderer('0,000.00'),
            align: 'right',
            width:70
          }, {
            header: '申购类型',
            dataIndex: 'purchase_type',
            renderer:purchaseTypeRender,
            width:80
          }, {
            header: '申购时间',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width:110
          }, {
            header: '部门<br>审批',
            dataIndex: 'dept_auditing',
            renderer:auditingRender,
            width:50
          }, {
            header: '最终<br>审批',
            dataIndex: 'auditing_status',
            renderer:auditingRender,
            width:50
          }]);
          
        var showPurhcase = new Ext.Button({
          text: '查看申购单',
          iconCls: 'viewItem',
          handler : function(){
            var records = purchaseGrid.getSelectionModel().getSelections();
            var dept_auditing_status = records[0].get('dept_auditing');
            if (dept_auditing_status.indexOf('未审批') > -1){
              //等于1代表未审批，其它数值为审批过的结果。
              dept_auditing_status = 0;
            }else{
              dept_auditing_status = 1
            }
            
            if(records.length==0){
              Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
            }else{
              //把表单添加到窗口中
              purchaseId = records[0].get('id');
              purchaseViewWin = new Ext.Window({
                layout: 'fit',
                width:775,
                height:385,
                closeAction: 'destroy',
                plain: true,
                modal: true,
                constrain:true,
                //modal: true,
                title: '查看申购单',
                autoLoad: {
                  url: "/purchase/purchaseView?id="+purchaseId+"&auditing_status=<%=params[:auditing_status]%>&dept_auditing_status="+dept_auditing_status,
                  scripts: true
                }
              });
              purchaseViewWin.on("destroy",function(){
                purchaseDS.reload();
              });
              purchaseViewWin.resizable = false;
              purchaseViewWin.show();
            }

          }
        })

        var newPurchase = new Ext.Button({
          text: '新增申购单',
          iconCls: 'addItem',
          handler : function(){
            newPurchaseWin = new Ext.Window({
              layout: 'fit',
              width:775,
              height:380,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              //modal: true,
              title: '新增申购单',
              autoLoad: {
                url: "/purchase/newPurchase",
                scripts: true
              }
            });
            newPurchaseWin.on("destroy",function(){
              purchaseDS.reload();
            });
            newPurchaseWin.resizable = false;
            newPurchaseWin.show();
          }
        })
        var purchaseGrid =  new Ext.grid.GridPanel({
          ds: purchaseDS,
          cm: purchaseCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-370,
          height: screenHeight-430,
          iconCls: 'icon-grid',loadMask: true,
          loadMask: true,
          tbar: [newPurchase,'-',showPurhcase,'-'],
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: purchaseDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        //如果当前页面不是“所有申购单”，则不显示“新建申购单”按钮。
        //统一为一个总的申购页面后，此处理屏蔽2011-08-15，jerry
        /*if(status != 'all'){
          newPurchase.setVisible(false);
        }*/
        purchaseDS.load({params:{start:0,limit:pageSize}});
        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-350,
          height: screenHeight-300,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[searchPurchaseForm,purchaseGrid]
        });
        workbenchPanel.render("workbench_panel_applicant");
        
      })
    </script>
  </head>
  <body>
    <div id="workbench_panel_applicant"></div>
  </body>
</html>
